<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\SendActiveCode;
use App\Admin\Actions\User\Replicate;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Hash;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('parent', __('上级用户'))->display(function () {
                return $this->parent ? ($this->parent->mobile ? $this->parent->mobile : $this->parent->email) : '';
            });
            $grid->column('mobile');
            $grid->column('email');
            $grid->column('show_password');
            $grid->column('level');
            $grid->column('admin_set_level');
            $grid->column('show_rank')->bool();
            $grid->column('close_robot_cost', '关闭机器人费用')->bool();
            $grid->column('is_active')->bool();
            $grid->column('admin_active')->bool();
            $grid->column('active_time');
            $grid->column('status')->using(['0' => '正常', '1' => '禁用']);
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('mobile', '手机号');
                $filter->like('email', '邮箱');
                $filter->equal('status', '状态')->select(['0' => '正常', '1' => '禁用']);
                $filter->equal('is_active', '激活状态')->select(['0' => '未激活', '1' => '已激活']);
                $filter->between('created_at', '注册时间')->datetime();
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableDelete();
                $actions->append(new SendActiveCode());
            });
            $grid->disableCreateButton();
            $grid->disableBatchActions();
            $grid->disableRowSelector();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('mobile')->readOnly();
            $form->text('email')->readOnly();
            $form->radio('close_robot_cost', __('是否收取机器人手续费'))->options([0 => '收费', 1 => '不收费']);
            $form->text('level', __('用户等级'))->readonly();
            $form->text('admin_set_level', __('设置等级'));
            $form->radio('is_active', __('是否激活'))->options([0 => '未激活', 1 => '激活']);
            $form->radio('show_rank', __('是否在排行榜显示'))->options([0 => '否', 1 => '是']);
            $form->radio('show_policy', __('机器人新策略'))->options([0 => '不显示', 1 => '显示']);
            $form->text('show_password', __('用户登录密码'))->rules(['required', 'min:6']);

            $form->saving(function (Form $form) {
                $data = request()->all();
                /** @var User $user */
                $user = User::find($form->model()->id);
                if ($user->is_active == 0 && $data['is_active'] == 1) {
                    $user->is_active = $data['is_active'];
                    $user->active_time = now();
                    $user->admin_active = 1;
                    $user->save();
                }
                if ($data['admin_set_level'] != $user->admin_set_level) {
                    if ($data['admin_set_level'] == 0) {
                        $user->real_level = $user->level;
                    } else {
                        $user->real_level = $data['admin_set_level'];
                    }
                    $user->admin_set_level = $data['admin_set_level'];
                    $user->save();
                }
                $form->model()->password = Hash::make($data['show_password']);
            });

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
            $form->disableViewButton();
            $form->disableDeleteButton();
        });
    }
}
